package com.jungle.sp.daily;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class ImageDimensions {
    public static void main(String[] args) {
        try {
            // 定义图片的URI
            URI uri = new URI("https://s3.ap-east-1.amazonaws.com/ysvideo/20240722/76c56e17437b457d5b3bd0ba8d6dad8512af52bb3e992e54207aff65327584ec.jpg");

            // 创建HttpClient
            HttpClient client = HttpClient.newBuilder()
                    .version(HttpClient.Version.HTTP_2)
                    .build();

            // 创建HttpRequest
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(uri)
                    .GET()
                    .build();

            // 发送请求并获取响应
            HttpResponse<InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());

            // 检查HTTP响应码
            if (response.statusCode() == 200) { // HTTP OK
                // 读取图片
                BufferedImage image = ImageIO.read(response.body());

                if (image != null) {
                    // 获取宽度和高度（单位：像素）
                    int width = image.getWidth();
                    int height = image.getHeight();

                    // 输出宽度和高度
                    System.out.println("Width: " + width + " pixels");
                    System.out.println("Height: " + height + " pixels");
                } else {
                    System.out.println("Could not load image.");
                }
            } else {
                System.out.println("Failed to fetch image. HTTP response code: " + response.statusCode());
            }
        } catch (IOException | InterruptedException | java.net.URISyntaxException e) {
            System.err.println("Error loading image: " + e.getMessage());
        }
    }
}
